// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.
using System.Windows;
using Utils;
using Utils.Documentation;

namespace Byond.PatchUiControls
{
	[TemplatePart(Name = "PART_InnerFill", Type = typeof(FrameworkElement))]
	[TemplatePart(Name = "PART_OuterFill", Type = typeof(FrameworkElement))]
	[Doc]
	public class RangeSlider : RangeSliderBase
	{
		static RangeSlider()
		{
			DefaultStyleKeyProperty.OverrideMetadata(
				typeof(RangeSlider),
				new FrameworkPropertyMetadata(typeof(RangeSlider)));
		}

		private FrameworkElement _innerFill = new FrameworkElement();
		private FrameworkElement _outerFill = new FrameworkElement();

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_innerFill = GetTemplatePart<FrameworkElement>("PART_InnerFill");
			_outerFill = GetTemplatePart<FrameworkElement>("PART_OuterFill");
			_outerFill.SizeChanged += (s, e) => UpdateHeight();
		}

		protected override void OnValuesChanged()
		{
			UpdateHeight();
		}

		private void UpdateHeight()
		{
			var bottom = MathHelper.Map(Min, 0, Max, _outerFill.ActualHeight, ValueFrom);
			var height = MathHelper.Map(Min, 0, Max, _outerFill.ActualHeight, ValueTo) - bottom;

			_innerFill.Margin = new Thickness(
				0, 0, 0, double.IsNaN(bottom) || bottom < 0 ? 0 : bottom);
			var minVisualHeight = IsSimpleSlider ? 0 : 0; // doch beides 0...
			_innerFill.Height = height <= 0 ? minVisualHeight : height;
		}
	}
}